package com.shuhua.taskServer.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shuhua.pojo.entity.TaskComment;
import com.shuhua.taskServer.service.TaskCommentService;
import com.shuhua.common.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/taskComment")
public class TaskCommentController {

    @Autowired
    private TaskCommentService taskCommentService;

    private static final String[] TASK_COMMENT_FIELDS = {"id", "task_id", "commentman_id", "content", "created_at"};

    @PostMapping
    public Result<?> add(@RequestBody TaskComment taskComment) {
        taskCommentService.save(taskComment);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
        taskCommentService.removeById(id);
        return Result.success();
    }

    @PutMapping
    public Result<?> update(@RequestBody TaskComment taskComment) {
        taskCommentService.updateById(taskComment);
        return Result.success();
    }

    private Map<String, Object> wrapCraftsman(Map<String, Object> map) {
        if (map == null) return null;
        Map<String, Object> craftsman = new java.util.HashMap<>();
        java.util.LinkedHashMap<String, Object> result = new java.util.LinkedHashMap<>();
        // 1. 按实体类顺序输出主表字段
        for (String field : TASK_COMMENT_FIELDS) {
            if (map.containsKey(field)) {
                result.put(field, map.get(field));
            }
        }
        // 2. 其他非工匠字段补充
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String key = entry.getKey();
            if (!key.startsWith("craftsman_") && !result.containsKey(key)) {
                result.put(key, entry.getValue());
            }
            if (key.startsWith("craftsman_")) {
                craftsman.put(key.substring(10), entry.getValue());
            }
        }
        // 3. 工匠信息放最后
        result.put("craftsman", craftsman);
        return result;
    }

    @GetMapping("/{id}")
    public Result<Map<String, Object>> get(@PathVariable Long id) {
        Map<String, Object> comment = taskCommentService.getTaskCommentWithCraftsmanById(id);
        return Result.success(wrapCraftsman(comment));
    }

    @GetMapping("/list")
    public Result<List<Map<String, Object>>> list() {
        List<Map<String, Object>> list = taskCommentService.getAllTaskCommentWithCraftsman();
        list.replaceAll(this::wrapCraftsman);
        return Result.success(list);
    }

    /**
     * 根据任务ID查询所有评论
     */
    @GetMapping("/task/{taskId}")
    public Result<List<Map<String, Object>>> getByTaskId(@PathVariable Long taskId) {
        List<Map<String, Object>> comments = taskCommentService.getTaskCommentWithCraftsmanByTaskId(taskId);
        comments.replaceAll(this::wrapCraftsman);
        return Result.success(comments);
    }
} 